{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.risk import (\n",
    "    MarketDataShockBasedScenario,\n",
    "    MarketDataPattern,\n",
    "    MarketDataShock,\n",
    "    MarketDataShockType,\n",
    "    IRVegaParallel,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Risk measures are short-hand for a set of predefined scenarios. Let's look at how at to get the `IRVegaParallel` value using a `MarketDataShockBasedScenario`. We calculate delta as a 2-sided 1bp bump."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaption = IRSwaption(PayReceive.Pay, '10y', Currency.USD, notional_amount=10e6)\n",
    "swaption.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r_shock_bp = 1 / 10000\n",
    "ir_scenario_up = MarketDataShockBasedScenario(\n",
    "    {MarketDataPattern('IR Vol'): MarketDataShock(MarketDataShockType.Absolute, r_shock_bp)}\n",
    ")\n",
    "ir_scenario_down = MarketDataShockBasedScenario(\n",
    "    {MarketDataPattern('IR Vol'): MarketDataShock(MarketDataShockType.Absolute, -r_shock_bp)}\n",
    ")\n",
    "with PricingContext():\n",
    "    vega = swaption.calc(IRVegaParallel)\n",
    "    with ir_scenario_up:\n",
    "        up = swaption.dollar_price()\n",
    "    with ir_scenario_down:\n",
    "        down = swaption.dollar_price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined = (up.result() - down.result()) / 2\n",
    "# should give the same value\n",
    "combined - delta.result()\n",
    "\n",
    "print(f'Vega direct={vega.result():.0f}, Vega by shocks={((up.result() - down.result()) / 2):.0f}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
